/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package psw;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
 *
 * @author tabakkie
 */
public class StatusBar extends JPanel 
{
    private static final String THEME = "tegenwoordige tijd"; 
    private static final Integer JOKERS = 2;
    private static final Integer TIME = 200;
    private static final Integer POINTS = 1550;
    private boolean checkingAnswers = true;
    private int totalWidth;
    private int totalHeight;
    private RoundRectangle2D.Double outerBorder;
    private RoundRectangle2D.Double jokerIcon;
    private RoundRectangle2D.Double pointsIcon;
    private RoundRectangle2D.Double timeIcon;
    private RoundRectangle2D.Double themeBar;
    
    private int iconWidth;
    private int iconHeight;
    private int iconCorner;
    private int spacer;
    
    private BufferedImage jokerImage;
    private BufferedImage pointsImage;
    private BufferedImage timeImage;    
    private int imgHeight;
    
    public StatusBar(int frameWidth, int frameHeight)
    {
        setOpaque(false);
        setLayout(null);
        //setBorder(BorderFactory.createLineBorder(Color.black, 1));
        outerBorder = new RoundRectangle2D.Double();
        jokerIcon = new RoundRectangle2D.Double();
        pointsIcon = new RoundRectangle2D.Double();
        timeIcon = new RoundRectangle2D.Double();
        themeBar = new RoundRectangle2D.Double();                        
        resizeStatusBar(frameWidth, frameHeight);
    }

    private void resizeStatusBar(int frameWidth, int frameHeight)
    {
        totalWidth = (int) (Math.min(frameWidth, frameHeight)/2.3);
        totalHeight = (int) (Math.min(frameWidth, frameHeight)/4);
                
        spacer = totalHeight/30;
        iconWidth = (totalWidth - (spacer * 4)) / 3;
        iconHeight = (int) ((totalHeight - (spacer * 3)) / 1.5);
        iconCorner = iconHeight / 4;                        
        createJokerIcon();
        createPointsIcon();
        createTimeIcon();
        createThemeBar();  
        setSize(totalWidth, totalHeight);
        
        imgHeight = (int) (iconHeight / 1.7);
        
    }
    
    private int getImageWidth(BufferedImage image)
    {
        if (image != null) {            
            return image.getWidth()*imgHeight/image.getHeight();
        }
        else {
            return 0;
        }           
    }
    
    private void createJokerIcon()
    {        
        int xIcon =  spacer;
        int yIcon =  spacer;
        jokerIcon.setRoundRect(xIcon, yIcon, iconWidth, iconHeight, iconCorner, iconCorner);  
                
        try {
            jokerImage = ImageIO.read(new File("joker_72DPI.png"));
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private void createPointsIcon()
    {
        int xIcon = (spacer * 2) + iconWidth;
        int yIcon =  spacer;
        pointsIcon.setRoundRect(xIcon, yIcon, iconWidth, iconHeight, iconCorner, iconCorner);
        
        try {
            pointsImage = ImageIO.read(new File("geld_72DPI.png"));
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private void createTimeIcon()
    {
        int xIcon = (spacer * 3) + (iconWidth * 2);
        int yIcon =  spacer;
        timeIcon.setRoundRect(xIcon, yIcon, iconWidth, iconHeight, iconCorner, iconCorner);
        
        try {
            timeImage = ImageIO.read(new File("clock_72DPI.png"));
        } 
        catch (IOException e) {
            e.printStackTrace();
        }        
    }
    
    private void createThemeBar()
    {
        int xBar = spacer;
        int yBar = (spacer * 2) + iconHeight;
        int barWidth = totalWidth - (spacer * 2);
        int barHeight = (totalHeight - (spacer * 3)) / 3;
        themeBar.setRoundRect(xBar, yBar, barWidth, barHeight, iconCorner, iconCorner);
    }
    
    private BufferedImage scaleImage(BufferedImage img, int width, int height) 
    {
        int imgWidth = img.getWidth();
        int imgHeight = img.getHeight();
        if (imgWidth*height < imgHeight*width) {
            width = imgWidth*height/imgHeight;
        } 
        else {
            height = imgHeight*width/imgWidth;
        }
        BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = newImage.createGraphics();
        try {
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            
            g.drawImage(img, 0, 0, width, height, null);
        } finally {
            g.dispose();
        }
        return newImage;
    }
    
    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(totalWidth, totalHeight);
    }
    
    @Override
    protected void paintComponent(Graphics g) 
    {
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Rectangle2D rect = null;
        FontMetrics fm = null;
        Font font = null;
        int textWidth;
        int itemWidth;
        int xString;
        int yString;
       // g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2.setStroke(new BasicStroke(2));        
        g2.setColor(Color.black);
        g2.draw(outerBorder);
        
        GradientPaint gp = new GradientPaint(0, 0, Color.YELLOW, StatusBar.this.getWidth(), StatusBar.this.getHeight(), Color.ORANGE);
        if(checkingAnswers) {
            //========================== Joker Icon ==========================
            g2.setPaint(gp);
            g2.fill(jokerIcon);
            g2.setColor(Color.black);
            g2.draw(jokerIcon);
            int jokerWidth = getImageWidth(jokerImage);
            int xJoker = (int) ((jokerIcon.getX() + (iconWidth / 2)) - (jokerWidth / 2));
            int yJoker = spacer * 2;        
            g2.drawImage(jokerImage, xJoker, yJoker, jokerWidth, imgHeight, null);
            System.out.println("Joker: w: " +jokerWidth + "  h: " + imgHeight);
            //------------- jokerIcon tekst -------------
            g2.setColor(Color.black);         
            font = new Font("Arial Black", Font.PLAIN, (int)(totalHeight/5.5));
            fm   = g2.getFontMetrics(font);
            rect = fm.getStringBounds(JOKERS.toString(), g2);       

            xString = (int) (jokerIcon.getX() + ((jokerIcon.getWidth()  - rect.getWidth())  / 2));
            yString = (int) (jokerIcon.getY() + jokerIcon.getHeight() - (spacer * 1.2));
            g2.setFont(font);
            g2.drawString(JOKERS.toString(), xString, yString); 
            //------------------------------------------    
            //================================================================
            
            //========================== Points Icon ==========================
            g2.setPaint(gp);
            g2.fill(pointsIcon);
            g2.setColor(Color.black);
            g2.draw(pointsIcon);
            int pointsWidth = getImageWidth(pointsImage);
            int xPoints = (int) ((pointsIcon.getX() + (iconWidth / 2)) - (pointsWidth / 2));
            int yPoints = spacer * 2;        
            g2.drawImage(pointsImage, xPoints, yPoints, pointsWidth, imgHeight, null);
            System.out.println("Money: w: " +pointsWidth + "  h: " + imgHeight);
            //------------- points icon tekst -------------
            g2.setColor(Color.black);         
            font = new Font("Arial Black", Font.PLAIN, (int)(totalHeight/5.5));
            fm   = g2.getFontMetrics(font);
            rect = fm.getStringBounds(POINTS.toString(), g2);       

            xString = (int) (pointsIcon.getX() + ((pointsIcon.getWidth()  - rect.getWidth())  / 2));
            yString = (int) (pointsIcon.getY() + pointsIcon.getHeight() - (spacer * 1.2));
            g2.setFont(font);
            g2.drawString(POINTS.toString(), xString, yString); 
            //------------------------------------------    
            //================================================================
        }
        //========================== Time Icon ==========================
        g2.setPaint(gp);
        g2.fill(timeIcon);
        g2.setColor(Color.black);
        g2.draw(timeIcon);
        int timeWidth = getImageWidth(timeImage);
        int xTime = (int) ((timeIcon.getX() + (iconWidth / 2)) - (timeWidth / 2));
        int yTime = spacer * 2;        
        g2.drawImage(timeImage, xTime, yTime, timeWidth, imgHeight, null);
        System.out.println("Clock: w: " +timeWidth + "  h: " + imgHeight);
        //------------- timeIcon tekst -------------
        g2.setColor(Color.black);         
        font = new Font("Arial Black", Font.PLAIN, (int)(totalHeight/5.5));
        fm   = g2.getFontMetrics(font);
        rect = fm.getStringBounds(TIME.toString(), g2);       

        xString = (int) (timeIcon.getX() + ((timeIcon.getWidth()  - rect.getWidth())  / 2));
        yString = (int) (timeIcon.getY() + timeIcon.getHeight() - (spacer * 1.2));
        g2.setFont(font);
        g2.drawString(TIME.toString(), xString, yString); 
        //------------------------------------------    
        //================================================================
        
        //========================== Theme Bar ==========================
        g2.setPaint(gp);
        g2.fill(themeBar);
        g2.setColor(Color.black);
        g2.draw(themeBar);        
        //------------- themeBar tekst --------------------
        g2.setColor(Color.black);         
        if(THEME.length() < 11) {
            font = new Font("Arial Black", Font.PLAIN, totalHeight/4);
        }
        else if(THEME.length() == 11 || THEME.length() == 12) {
            font = new Font("Arial Black", Font.PLAIN, totalHeight/5);
        }
        else if(THEME.length() >= 13 && THEME.length() <= 15) {
            font = new Font("Arial Black", Font.PLAIN, totalHeight/6);
        }
        else if(THEME.length() >= 16) {
            font = new Font("Arial Black", Font.PLAIN, totalHeight/7);
        }
        fm   = g2.getFontMetrics(font);
        rect = fm.getStringBounds(THEME, g2);
        
        xString = (int) (themeBar.getX() + ((themeBar.getWidth()  - rect.getWidth())  / 2));
        yString = (int) (themeBar.getY() + (themeBar.getHeight() / 2) + (rect.getHeight()/4.8));
        g2.setFont(font);
        g2.drawString(THEME, xString, yString); 
        //------------------------------------------      
        //================================================================
        
        
    }
    
    
    
}
